package com.imooc.demo.Service.Impl;

import com.imooc.demo.Mapper.UsersMapper;
import com.imooc.demo.Model.users;
import com.imooc.demo.Service.UserService;
import com.imooc.demo.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UsersMapper userMapper;
    @Autowired
    private Sid sid;


    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public boolean queryUsernameIsExist(String username) {
        users user=new users();
        user.setUsername(username);
        users result=userMapper.selectOne(user);
        return result==null?false:true;
    }

//    @Autowired
//    private Sid sid;
    @Transactional(propagation=Propagation.REQUIRED)
    @Override
    public void saveUser(users user) {
//        String str=sid.nextShort();
//        int userId=Integer.parseInt(str);
//        user.setId(userId);
        userMapper.insert(user);
    }


    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public users queryUserForLogin(String username, String password) {
        Example example = new Example(users.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("username",username);
        criteria.andEqualTo("password",password);
        users user = userMapper.selectOneByExample(example);
        return user;
    }

//    @Override
////    public void updateUser(users user) {
////
////    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void updateUser(users user) {
        Example example = new Example(users.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id",user.getId());
        System.out.println(user.getId());
        userMapper.updateByExampleSelective(user,example);

    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public users queryUserInfo(Integer userId) {
        Example example = new Example(users.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id",userId);
        users users = userMapper.selectOneByExample(example);
        return users;
    }

//    @Transactional(propagation = Propagation.SUPPORTS)
//    @Override
//    public boolean isLikeVideo(String userId, String videoId) {
//        if (userId == null && videoId == null) {
//            return false;
//        }
//        Example example = new Example(UsersLikeVideos.class);
//        Criteria criteria = example.createCriteria();
//        criteria.andEqualTo("userId",userId);
//        criteria.andEqualTo("videoId",videoId);
//        List<UsersLikeVideos> list= usersLikeVideosMapper.selectByExample(example);
//        if (list != null && list.size() > 0) {
//            return true;
//        }
//        return false;
//    }
//
//    @Transactional(propagation = Propagation.REQUIRED)
//    @Override
//    public void becameFans(String userId, String fansId) {
//        String id = sid.nextShort();
//        UsersFans usersFans = new UsersFans();
//        usersFans.setId(id);
//        usersFans.setUserId(userId);
//        usersFans.setFanId(fansId);
//        usersFansMapper.insert(usersFans);
//        usersMapper.addFans(fansId);
//        usersMapper.addFollow(userId);
//    }
//
//    @Transactional(propagation = Propagation.REQUIRED)
//    @Override
//    public void cancelAttention(String userId, String fansId) {
//        Example example = new Example(UsersFans.class);
//        Criteria criteria = example.createCriteria();
//        criteria.andEqualTo("userId",userId);
//        criteria.andEqualTo("fanId",fansId);
//        usersFansMapper.deleteByExample(example);
//        usersMapper.reduceFans(fansId);
//        usersMapper.reduceFollow(userId);
//    }
//
//    @Override
//    public boolean isFollow(String userId, String fansId) {
//        Example example = new Example(UsersFans.class);
//        Criteria criteria = example.createCriteria();
//        criteria.andEqualTo("userId",userId);
//        criteria.andEqualTo("fanId",fansId);
//        List<UsersFans> list = usersFansMapper.selectByExample(example);
//        if (!list.isEmpty() && list.size() > 0) {
//            return true;
//        }
//        return false;
//    }

}
